var api = require('../../utils/api.js');
var http = require('../../utils/http.js');
var util = require('../../utils/util.js');
var app = getApp();

Component({

  /**
   * 页面的初始数据
   */
  data: {
    // url: app.globalData.url,
    isAuth: true
  },

  methods: {
    show: function() {
      this.setData({
        isAuth: false
      });
      app.login();
    },
    hid: function() {
      this.setData({
        isAuth: true
      });
    },
    bindGetUserInfo: function(e) {
      console.log(e)
      var that = this;
      var userInfo = e.detail.userInfo;
      var nickName = wx.setStorageSync("nickName", userInfo.nickName)

      if (userInfo) {
        const nickName = userInfo.nickName;
        const avatarUrl = userInfo.avatarUrl;
        const gender = userInfo.gender; // 性别 0：未知、1：男、2：女
        const province = userInfo.province;
        const city = userInfo.city;
        const country = userInfo.country;

        // app.globalData.nickName = nickName;
        // app.globalData.avatarUrl = avatarUrl;
        // app.globalData.gender = gender;
        // app.globalData.province = province;
        // app.globalData.city = city;
        // app.globalData.country = country;

        var token = wx.getStorageSync('token');
        console.log(token + "token");

        http.post(api.user_info_set, {
          token: token,
          nickName: nickName,
          avatarUrl: avatarUrl,
          gender: gender,
          province: province,
          city: city,
          country: country,
          parent_openid: wx.getStorageSync("parent_openid")
        }, function(res) {
          console.log(res.data, '++++++++++')
          if (res.data.code == 0) {

            that.setData({
              isAuth: false
            });

          } else {
            that.setData({
              isAuth: true
            });

            // util.msg(res);
            app.globalData.user = res.data.data;
            wx.setStorageSync('user_id', res.data.data.id)
            wx.setStorageSync('user_info', res.data)
            console.log(that.data.isAuth, '--------------')
            var myEventDetail = { // detail对象，提供给事件监听函数  //监听函数可以通过e.detail查看传递的数据;
              　　　　
              hidden: false,
              　　　　text: 1　　
            }　　
            var myEventOption = {

              　　} // 触发事件的选项
            that.triggerEvent('myevent', myEventDetail, myEventOption);

          }
        }, "登录中...");

        // api.setUserInfo({
        //     token: token,
        //     nickName: nickName,
        //     avatarUrl: avatarUrl,
        //     gender: gender,
        //     province: province,
        //     city: city,
        //     country: country
        // }, function (res) {
        //   if (res) {
        //     util.msg(res);
        //   } else {
        //     that.setData({
        //       isAuth: true
        //     });
        //   }
        // });
      } else {
        wx.setStorageSync('token', '');

        // util.msg('您取消了授权！');
      }
    }
  }
})